package cn.com.org.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
/*		HandlerMethod method = (HandlerMethod) handler;
		System.out.println(method.hasMethodAnnotation(ResponseBody.class)
				+ "<===============");
		;
		System.out.println(handler.getClass().isAssignableFrom(
				HandlerMethod.class));
		RequestMapping authPassport = ((HandlerMethod) handler)
				.getMethodAnnotation(RequestMapping.class);
		System.out.println(authPassport);
		System.out.println(authPassport.path()[0]);
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		System.out.println(handlerMethod.getMethod());
		System.out.println("=-=-=-=-=-=-=-=-=-=");

		System.out.println(request.getRequestURI()
				+ "-------------get into preHandle");
		System.out.println(request.getRequestURL() + ">>>>>>>>>>>>>>>>>>>>");
		String uri = request.getRequestURI();
		if (uri.indexOf("index") < 0 && uri.indexOf("code") < 0) {

			System.out.println("=========================");
			// response.sendRedirect(request.getContextPath()+"/index.jsp");
			String redirecturl = request.getContextPath() + "/index.jsp";
			System.out.println(redirecturl);
			// request.getRequestDispatcher(redirecturl).forward(request,
			// response);
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
			// response.sendRedirect(contextPath+"/login.htm?redirectURL=" +
			// URLEncoder.encode(url));
			return false;
		}*/

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
